home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5910 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  35 lines

  1. Newsgroups: comp.lang.c
  2. Path: thinkage.on.ca!atbowler
  3. From: atbowler@thinkage.on.ca (Alan Bowler)
  4. Subject: Re: How to get a random strin
  5. Message-ID: <Dn5E0J.GKL@thinkage.on.ca>
  6. Sender: news@thinkage.on.ca
  7. Organization: Thinkage Ltd.
  8. References: <4g19id$p7n@gail.ripco.com>
  9. Date: Wed, 21 Feb 1996 22:48:19 GMT
  10.  
  11. In article <4g19id$p7n@gail.ripco.com> mambuhl@ripco.com (Martin Ambuhl) writes:
  12. >chancl@nevada.edu (Clapton Chan) in <4fh5od$qq0@news.nevada.edu> asks:
  13. >
  14. >[A poor practice follows]
  15. >if you do not #include <time.h>, you need
  16. >    srand((unsigned)time(NULL));
  17. >                                           
  18.  
  19. Worse than poor.  time() returns a time_t which might not be an
  20. integer type.  The above code implicitly declares "time()" as returning
  21. int.  Adding the cast to unsigned will not change that fact that
  22. this implicit declaration means your code could be looking in the
  23. wrong place.  For example:  Suppose "time_t" is actually "double"
  24. and not "int", and that the machine uses has separate floating
  25. point and integer registers.  (x86, pdp-11, ibm/370).  Then the
  26. above code could well result in the same vale being passed to srand()
  27. every time the program is called, because time() set the floating
  28. point result register and the above code is looking at an integer
  29. result register which untouched by time().
  30.  
  31. Aside:
  32.     Chosing double for time_t and clock_t ss likely to become a more
  33. common choice of implementors in the future.  It simplifies a lot of
  34. code.
  35.